package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.PackageMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.PackageService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class PackageServiceItml implements PackageService {
    @Autowired
    private PackageMapper packageMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    //根据分类id查询菜品
    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
        //封装好一个Dish对象
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);//根据categoryId查询菜品
        dish.setStatus(1);//只查询status=1的菜品

        List<Dish> list = packageMapper.list(dish);
        return list;
    }

    //套餐新增
    @Override
    @Transactional
    public void addPackage(SetmealDTO setmealDTO) {
        //copy setmealDTO到setmeal中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        LocalDateTime now = LocalDateTime.now();
        setmeal.setCreateTime(now);
        setmeal.setUpdateTime(now);
        setmeal.setCreateUser(10L);
        setmeal.setUpdateUser(10L);
        //将dish中的数据添加到setmeal表中
        setmealMapper.addPackage(setmeal);
        //从setmealDTO中拿出套餐菜品关系集合进行遍历将setmeal中的id赋值给setmeal_dish建立联系确认主键id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        //将setmealDTO中的数据添加到setmeal_dish中
        setmealDishMapper.addSetmealDish(setmealDishes);
    }
    //分页查询套餐
    @Override
    public PageResult selectPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = packageMapper.selectPage(setmealPageQueryDTO);
        List result = page.getResult();
        long total = page.getTotal();
        return new PageResult(total,result);
    }
    //根据id删除套餐
    @Override
    @Transactional
    public void deletePage(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            Integer status = setmeal.getStatus();
            if (status==1){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            //删除两个表的数据
            for (Long setmealId : ids) {
                //删除套餐表中的数据
                setmealMapper.deleteById(setmealId);
                //删除套餐菜品关系表中的数据
                setmealDishMapper.deleteById(setmealId);
            }

        }
    }
    //根据id查询套餐
    @Override
    public SetmealVO selectById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectSetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }
    //修改套餐
    @Override
    public void putSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐
        LocalDateTime now = LocalDateTime.now();
        setmeal.setUpdateTime(now);
        setmealMapper.putSetmeal(setmeal);

        //修改菜品关系表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setmealDishMapper.putSetmealDish(setmealDishes);

    }


}
